<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\AuthGroup;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Chargedep extends Backend
{

    /**
     * Chargedep模型对象
     * @var \app\admin\model\Chargedep
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Chargedep;
        $this->relationtTable = 'group';
        $groupList = collection(AuthGroup::where('id', 'neq',1)->select())->toArray();
        $groupdata = [];
        foreach ($groupList as $k => $v) {
            $groupdata[$v['id']] = $v['name'];
        }
        $this->view->assign('groupdata', $groupdata);

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function getSelectList(){
        $where = [];
        $group_id = $this->request->get('group_id');
        if($group_id)
            $where['group_id'] = $group_id;
        $list = $this->model->where($where)->select();
        $this->success('列表','',$list);
        // echo json_encode($list);
    }



    /**
     * 项目库搜索，获取街道全部
     */
    public function street()
    {
        // 如果当前是街道账号，只获取自己的
        $groupIds = $this->auth->getGroupIds();
        if(in_array(GROUP_STREET,$groupIds)){
            $this->whereExtend['name'] = $this->auth->nickname;
        }

        // $this->sortExtend = 'id';

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            // if ($this->request->request('keyField')) {
            //     return $this->selectpage1();
            // }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                // ->with($this->relationtTable)
                ->where($where)
                ->where($this->whereExtend)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                // ->with($this->relationtTable)
                ->where($where)
                ->where($this->whereExtend)
                // ->order($this->sortExtend)
                ->order('sort asc,id asc')
                ->limit($offset, $limit)
                ->select();
            
            // echo $this->model->getLastSql();
            // exit();
                


            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }




}
